*****************************************************************
*																*
*				Title: regressions_paper_app		 	 	   	*		
*															    *
*****************************************************************

/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we run regressions for the paper sector for the appendix and output the results
	 
		
+ Inputs:

		* 1. panel_paper.dta
		
==============================  TOP MATTER ==============================*/

	*set paths
	global proc_data "`pdir'/data"
	global output "`pdir'/outputs/appendix"

********************************** END HEADER **********************************

use "$proc_data/panel_paper.dta", clear


******************************************************************
**********************   APPENDIX TABLES	**********************
******************************************************************

************ Table A.4 Summary Statistics 

*Some rounding is applied by hand after generating the table, as the frmttable command is not flexible in adjusting digits after the comma

*************************

* make empty matrix
mat table_a4_a = J(5,10,.)

** Panel A, LHS, Around 1800:

local counter = 1

foreach x of varlist logYoverL_real L_total {	
	sum `x' if year==1794
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a4_a[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel A RHS: starts in column 6

local counter = 1

foreach x of varlist logYoverL_real L_total dummy_water_mill dummy_steam_mill dummy_other_mill  {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a4_a[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

matlist table_a4_a


frmttable using "$output/Tables/Table_A4A.tex", statmat(table_a4_a) replace ///
	landscape tex fragment ///
	rtitles("Log (output per worker)" \ "Number of workers" ///
	\ "Water power" \ "Steam power" \ "Other power") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
			
  preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A4A.tex"
	local tex2 "$output/Tables/Table_A4A.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Log","Log")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 


*************************

* make empty matrix
mat table_a4_b = J(6,10,.)

** Panel B, LHS, Around 1800:
local counter = 1

foreach x of varlist highsflow closecoal share_forest lnoutputdensity_commune lnconscripts_pc near_battles10 {	
	sum `x' if year==1794
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a4_b[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel B RHS: starts in column 6
local counter = 1

foreach x of varlist highsflow closecoal share_forest lnoutputdensity_commune lnconscripts_pc near_battles10 {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a4_b[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
}

matlist table_a4_b

frmttable using "$output/Tables/Table_A4B.tex", statmat(table_a4_b) replace ///
	landscape tex fragment ///
	rtitles("Access to high streamflow" \ "Proximity to coal" ///
	\ "Share of forest area" \ "Production density" \ "log(conscripts pc)" \ "Near Battles") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
	
 
  preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A4B.tex"
	local tex2 "$output/Tables/Table_A4B.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Access to high","Access to high")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 


*************************

mat table_a4_c = J(2,10,.)

** Panel A, LHS, Around 1800:
local counter = 1

foreach x of varlist distance_above_p90_1800 distance_london {	
	sum `x' if year==1794
	local stats "N mean sd min max"
	local cols = 1	
	foreach stat of local stats {
		mat table_a4_c[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
} 

** Panel A RHS: starts in column 6
local counter = 1
sum distance_above_p90_1840 distance_london if year==1840	

foreach x of varlist distance_above_p90_1800 distance_london {
	sum `x' if year==1840
	local stats "N mean sd min max"
	local cols = 6	
	foreach stat of local stats {
		mat table_a4_c[`counter',`cols'] = r(`stat')
		local cols = `cols' + 1
	}
	local counter = `counter' + 1
}

matlist table_a4_c

frmttable using "$output/Tables/Table_A4C.tex", statmat(table_a4_c) replace ///
	landscape tex fragment ///
	rtitles("Dist to p90" \ "Dist to London") ///
	sdec(0 , 2 , 2 , 2 , 2,0,2) 
	
	
preserve 
	clear
	set obs 1
	local tex "$output/Tables/Table_A4C.tex"
	local tex2 "$output/Tables/Table_A4C.tex"
	generate strL s = fileread("`tex'") if fileexists("`tex'")
	assert filereaderror(s)==0

	*removing the beggining and end to only insert the numbers and rows in stata
	replace s = ustrregexrf(s,"(?s).*Dist to p90","Dist to p90")
	replace s = ustrregexrf(s,"\\noalign.*(?s)","")
	replace s = ustrregexrf(s,"\\end.*(?s)","")
	gen byte fw = filewrite("`tex2'",s,1)
restore	 



************	Table A.7 (Panel A). ***********************************
*** Annual Productivity Growth (in %) at Different Quantiles of the Distribution	
*** same as Table 1C

estimates clear	
quietly{
	eststo: xi:reg logYoverL_real_pa i.year,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year, quantile(90) vce(robust)
	
}

estout using "$output/Tables/Table_A7A.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Paper milling\ (1794-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]")  varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table"

		
************	Table A.7: Alternative Productivity Measures in Paper Milling (Panel B)   ************
		
estimates clear	
quietly{
	eststo: xi:reg logYoverL_men_real_pa i.year,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_men_real_pa i.year, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_men_real_pa i.year, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_men_real_pa i.year, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_men_real_pa i.year, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_men_real_pa i.year, quantile(90) vce(robust)
	
}
*
estout using "$output/Tables/Table_A7B.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Paper milling\ (1794-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table"
		

		

********************************* Table A.18 (column 3) **********************************
** exit in 1794 		
** needs to be manually put together for the paper

estimates clear
quietly {

	reg logYoverL_real exit_1794 if year == 1794 , r
	
	}
	
estout using "$output/Tables/Table_A18_paper.tex", replace label drop (_cons) ///
rename (exit_1794 "Exit dummy") ///
 prefoot("\hline") stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(2 %9.0g) labels("R$^2$" "N")) ///
 mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.1cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.2cm] ")) ///
 cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(Paper) 	 	

		
		
		
************	Table A.22 (Panel C) *******************************************	
***** Productivity Growth by Quantiles – Controlling for Region FE    

estimates clear
quietly{
	eststo: xi:reg logYoverL_real_pa i.year i.id_region,  vce(robust)
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year i.id_region, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year i.id_region, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year i.id_region, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year i.id_region, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year i.id_region, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A22C.tex", replace label drop( _Iid_region_*) order($show ) ///
rename (_Iyear_1840 "Paper milling\ (1794-1840)")   ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) ///
	indicate("Region FE=_cons", labels("\multicolumn{1}{c}{\checkmark}" "\multicolumn{1}{c}{ }"))				
*redundancy in reported number of observations in the last column is fixed by hand after generating the table




************	Table A.25 Cols 3 and 6:    ***********************************	
** needs to be manually put together for the paper	
** Correlation between logY/L and conscripts/capita **********************	

estimates clear	
eststo: reg logYoverL_real lnconscripts_pc if year==1794, cl(id_region)
eststo: reg logYoverL_real lnconscripts_pc if year==1840, cl(id_region)
	
estout using "$output/Tables/Table_A25_paper.tex", replace label drop(_cons) order($show ) ///
	stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(2 %9.0g) labels("R$^2$" "N")) ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
	

	
	
************	Table A.26 Col 3     ******************************************
** needs to be manually put together for the paper		
** Correlation between logY/L and battles within 10k in 1840

estimates clear
	eststo: reg logYoverL_real near_battles10 if year==1840, vce(robust)
	
estout using "$output/Tables/Table_A26_paper.tex", replace label drop(_cons) order($show ) ///
mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
stats(r2 N, layout(@ "\multicolumn{1}{c}{@}") fmt(2 %9.0g) labels("R$^2$" "N")) ///
cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 
	
	
	

************	Table A.27 (Panel C): ******************************************
*** Productivity Growth by Quantiles – Plants with at Least 10 Workers    

estimates clear	
quietly{
	eststo: xi:reg logYoverL_real_pa i.year if L_total>=10,  vce(robust)	
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year if L_total>=10, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year if L_total>=10, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year if L_total>=10, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year if L_total>=10, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year if L_total>=10, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A27C.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Paper milling\ (1794-1840)")  ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table	
		
	
	
	
************	Table A.29 (Panel C): ****************************************	
*** Productivity by Quantiles – Controlling for Number of Workers    

	
estimates clear
quietly{
	eststo: xi:reg logYoverL_real_pa i.year logL_total,  vce(robust)	
	local N_obs = e(N)
	eststo: xi:qreg logYoverL_real_pa i.year logL_total, quantile(10) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year logL_total, quantile(25) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year logL_total, quantile(50) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year logL_total, quantile(75) vce(robust)
	eststo: xi:qreg logYoverL_real_pa i.year logL_total, quantile(90) vce(robust)
}
estout using "$output/Tables/Table_A29C.tex", replace label drop(_cons) order($show ) ///
rename (_Iyear_1840 "Paper milling\ (1794-1840)")   ///
	mlabels(, title prefix(%) begin(%)) postfoot("\addlinespace[0.25cm] ") end("& `N_obs'" "\\[-0.15cm]") varlabels(, end("\addlinespace[0.00cm] ")) ///
	cells(b(fmt(3) star) se(fmt(3) par)) starlevels($^{*}$ 0.1 $^{**}$ 0.05 $^{***}$ 0.01) style(tex) collabels(none) 		
*redundancy in reported number of observations in the last column is fixed by hand after generating the table	
	
	
	
	
	
	